import os
import configparser
from pathlib import Path

# 获取配置文件路径
config_file = os.path.join(os.path.dirname(__file__), 'conf/config.ini')

# 创建配置解析器
config = configparser.ConfigParser()

# 如果配置文件不存在，则创建默认配置
if not os.path.exists(config_file):
    # 创建默认配置
    config['DEFAULT'] = {
        'SECRET_KEY': 'your_secret_key_here',
        'API_PREFIX': '/api/v1',
        'MAX_CONTENT_LENGTH': str(200 * 1024 * 1024),  # 200MB
        'API_DIR': 'api_scripts',
        'LOG_FILE': 'api_access.log',
        'JWT_SECRET': 'your_jwt_secret_here',
        'JWT_ALGORITHM': 'HS256',
        'JWT_EXPIRE': '3600',
        'MYSQL_HOST': 'localhost',
        'MYSQL_USER': 'root',
        'MYSQL_PASSWORD': 'asdf',
        'MYSQL_DB': 'test',
        'SCRIPT_STORAGE': 'database',
        'SCRIPTS_TABLE': 'api_scripts',
        'METADATA_TABLE': 'api_metadata',
        'OSS_ACCESS_KEY_ID': 'OSS_ACCESS_KEY_ID',
        'OSS_ACCESS_KEY_SECRET' : 'access_key_secret',
        'OSS_ENDPOINT':'endpoint',
        'OSS_BUCKET_NAME':'bucket_name'
    }

    # 使用 UTF-8 编码写入配置文件
    with open(config_file, 'w', encoding='utf-8') as f:
        config.write(f)
else:
    # 使用 UTF-8 编码读取现有配置文件
    with open(config_file, 'r', encoding='utf-8') as f:
        config.read_file(f)


class Config:
    # 从INI文件获取配置
    SECRET_KEY = config['DEFAULT'].get('SECRET_KEY', 'your_secret_key_here')
    API_PREFIX = config['DEFAULT'].get('API_PREFIX', '/api/v1')
    MAX_CONTENT_LENGTH = int(config['DEFAULT'].get('MAX_CONTENT_LENGTH', 200 * 1024 * 1024))
    API_DIR = config['DEFAULT'].get('API_DIR', 'api_scripts')
    LOG_FILE = config['DEFAULT'].get('LOG_FILE', 'api_access.log')

    # JWT配置
    JWT_SECRET = config['DEFAULT'].get('JWT_SECRET', 'your_jwt_secret_here')
    JWT_ALGORITHM = config['DEFAULT'].get('JWT_ALGORITHM', 'HS256')
    JWT_EXPIRE = int(config['DEFAULT'].get('JWT_EXPIRE', 3600))

    # MySQL数据库配置
    MYSQL_HOST = config['DEFAULT'].get('MYSQL_HOST', 'localhost')
    MYSQL_USER = config['DEFAULT'].get('MYSQL_USER', 'root')
    MYSQL_PASSWORD = config['DEFAULT'].get('MYSQL_PASSWORD', 'asdf')
    MYSQL_DB = config['DEFAULT'].get('MYSQL_DB', 'test')

    # 脚本存储方式配置
    SCRIPT_STORAGE = config['DEFAULT'].get('SCRIPT_STORAGE', 'database')

    # 数据库表名
    SCRIPTS_TABLE = config['DEFAULT'].get('SCRIPTS_TABLE', 'api_scripts')
    METADATA_TABLE = config['DEFAULT'].get('METADATA_TABLE', 'api_metadata')

    #获取OSS配置
    OSS_ACCESS_KEY_ID = config['DEFAULT'].get('OSS_ACCESS_KEY_ID', 'OSS_ACCESS_KEY_ID')
    OSS_ACCESS_KEY_SECRET = config['DEFAULT'].get('OSS_ACCESS_KEY_SECRET', 'OSS_ACCESS_KEY_SECRET')
    OSS_ENDPOINT= config['DEFAULT'].get('OSS_ENDPOINT', 'OSS_ENDPOINT')
    OSS_BUCKET_NAME= config['DEFAULT'].get('OSS_BUCKET_NAME', 'OSS_BUCKET_NAME')


# 可选：添加一个函数来重新加载配置（如果需要动态更新配置）
def reload_config():
    """重新加载配置文件"""
    global config
    with open(config_file, 'r', encoding='utf-8') as f:
        config.read_file(f)

    # 更新Config类的属性
    Config.SECRET_KEY = config['DEFAULT'].get('SECRET_KEY', 'your_secret_key_here')
    Config.API_PREFIX = config['DEFAULT'].get('API_PREFIX', '/api/v1')
    Config.MAX_CONTENT_LENGTH = int(config['DEFAULT'].get('MAX_CONTENT_LENGTH', 200 * 1024 * 1024))
    Config.API_DIR = config['DEFAULT'].get('API_DIR', 'api_scripts')
    Config.LOG_FILE = config['DEFAULT'].get('LOG_FILE', 'api_access.log')
    Config.JWT_SECRET = config['DEFAULT'].get('JWT_SECRET', 'your_jwt_secret_here')
    Config.JWT_ALGORITHM = config['DEFAULT'].get('JWT_ALGORITHM', 'HS256')
    Config.JWT_EXPIRE = int(config['DEFAULT'].get('JWT_EXPIRE', 3600))
    Config.MYSQL_HOST = config['DEFAULT'].get('MYSQL_HOST', 'localhost')
    Config.MYSQL_USER = config['DEFAULT'].get('MYSQL_USER', 'root')
    Config.MYSQL_PASSWORD = config['DEFAULT'].get('MYSQL_PASSWORD', 'asdf')
    Config.MYSQL_DB = config['DEFAULT'].get('MYSQL_DB', 'test')
    Config.SCRIPT_STORAGE = config['DEFAULT'].get('SCRIPT_STORAGE', 'database')
    Config.SCRIPTS_TABLE = config['DEFAULT'].get('SCRIPTS_TABLE', 'api_scripts')
    Config.METADATA_TABLE = config['DEFAULT'].get('METADATA_TABLE', 'api_metadata')